429964204 by Unknown

429964204 by Unknown

Author:Unknown
Language: eng
Format: epub


When you make a virtual function call through a base-class pointer the compiler quietly inserts code to fetch the VPTR and look up the function address in the VTABLE, thus calling the right function and causing late binding to take place.

Suppose a pointer to the base class object contains address of the derived class object and you call a virtual function using this pointer. Now something special happens. Instead of performing a typical function call, which is simply an assembly language CALL to a particular address, the compiler generates different code to perform the function call. The compiler starts with the contents of the base-class pointer. These contents are address of the derived class object. Using this address the VPTR of the derived class object is fetched. Using VPTR the VTABLE of the derived class is accessed. From this table the address of the function being called is extracted. Lastly using this address the function of the derived class is called.

All of this—setting up the VTABLE for each class, initializing the VPTR, inserting the code for the virtual function call—happens automatically, so you don't have to worry about it.

I am sure what we said here would seem pretty abstract unless we see it working in a program. So let us write one. Figure 7-1 shows the hierarchy of classes that we propose to implement in the program.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.